<template>
	<a-form ref="searchFormRef" name="advanced_search" class="ant-advanced-search-form" :model="searchFormState">
		<a-row :gutter="24">
			<a-col :span="6">
				<a-form-item name="ruleName" label="名称">
					<a-input v-model:value="searchFormState.ruleName" allow-clear placeholder="请输入名称"></a-input>
				</a-form-item>
			</a-col>
			<a-col :span="6">
				<a-form-item name="usedDate" label="使用日期">
					<a-input v-model:value="searchFormState.usedDate" allow-clear placeholder="请输入使用日期"></a-input>
				</a-form-item>
			</a-col>
			<a-col :span="6">
				<a-form-item name="used" label="已使用数量">
					<a-input-number
						v-model:value="searchFormState.used"
						allow-clear
						placeholder="请输入已使用数量"
						style="width: 100%"
					/>
				</a-form-item>
			</a-col>
			<a-col :span="6">
				<a-button type="primary" @click="table.refresh(true)">
					<template #icon><SearchOutlined /></template>查询</a-button
				>
				<a-button class="buttom-left" @click="() => searchFormRef.resetFields()">
					<template #icon><redo-outlined /></template>重置</a-button
				>
			</a-col>
		</a-row>
	</a-form>
	<s-table
		ref="table"
		:columns="columns"
		:data="loadData"
		:alert="options.alert.show"
		:scroll="{ x: '1000px' | true }"
		bordered
		:row-key="(record) => record.id"
		:row-selection="options.rowSelection"
		:tool-config="toolConfig"
	>
		<template #operator>
			<a-space>
				<a-button type="primary" @click="formRef.onOpen()" v-if="hasPerm('rulesLogAdd')">
					<template #icon><plus-outlined /></template>新增规则日志</a-button
				>
				<batch-delete
					v-if="hasPerm('rulesLogBatchDelete')"
					title="确定要删除选中规则日志吗?"
					buttonName="删除规则日志"
					:selectedRowKeys="selectedRowKeys"
					@batchDelete="batchDeleteRulesLog"
				/>
			</a-space>
		</template>
		<template #bodyCell="{ column, record }">
			<template v-if="column?.dataIndex === 'action'">
				<a-space>
					<a @click="formRef.onOpen(record)" v-if="hasPerm('rulesLogEdit')">编辑</a>
					<a-popconfirm title="确定要删除吗？" @confirm="deleteRulesLog(record)">
						<a-button type="link" danger size="small" v-if="hasPerm('rulesLogDelete')">删除</a-button>
					</a-popconfirm>
				</a-space>
			</template>
		</template>
	</s-table>
	<Form ref="formRef" @successful="table.refresh(true)" />
</template>

<script setup>
	import Form from './form.vue'
	import rulesLogApi from '@/api/dev/rulesLogApi'

	let searchFormState = reactive({}) //查询表单状态
	const searchFormRef = ref() //查询表单
	// 查询区域显示更多控制
	const formRef = ref() //新增/编辑表单页

	//表格配置
	const table = ref() //表格
	let selectedRowKeys = ref([]) //选中项
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false } //表格工具配置
	const columns = [
		{
			title: '名称',
			width: 100,
			dataIndex: 'ruleName'
		},
		{
			title: '使用日期',
			width: 100,
			dataIndex: 'usedDate'
		},
		{
			title: '已使用数量',
			width: 100,
			dataIndex: 'used'
		},
		{
			title: '创建人',
			width: 100,
			dataIndex: 'createUser'
		},
		{
			title: '创建时间',
			width: 100,
			dataIndex: 'createTime'
		}
	]
	if (hasPerm(['rulesLogEdit', 'rulesLogDelete'])) {
		columns.push({
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			fixed: 'right',
			width: 100
		})
	}

	// 列表选择配置
	const options = reactive({
		alert: {
			show: true, //显示选择多少个
			clear: () => {
				selectedRowKeys = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	})

	// 表格查询 返回 Promise 对象
	const loadData = (parameter) => {
		const searchFormParam = JSON.parse(JSON.stringify(searchFormState))
		return rulesLogApi.rulesLogPage(Object.assign(parameter, searchFormParam)).then((data) => {
			return data
		})
	}

	// 删除
	const deleteRulesLog = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		rulesLogApi.rulesLogDelete(params).then(() => {
			table.value.refresh(true)
		})
	}

	// 批量删除
	const batchDeleteRulesLog = (params, callback) => {
		rulesLogApi
			.rulesLogDelete(params)
			.then(() => {})
			.finally(() => {
				table.value.clearRefreshSelected()
				callback() //调用组件中的回调方法
			})
	}
</script>

<style scoped>
	.buttom-left {
		margin-left: 8px;
	}
</style>
